home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Source.bin / MovingAnimation.java < prev    next >
Text File  |  1998-08-21  |  8KB  |  250 lines

  1. package symantec.itools.multimedia;
  2.  
  3.  
  4. import java.awt.Dimension;
  5. import java.awt.Graphics;
  6. import java.awt.Image;
  7. import java.beans.PropertyVetoException;
  8. import java.beans.PropertyChangeListener;
  9. import java.beans.VetoableChangeListener;
  10. import java.beans.PropertyChangeEvent;
  11.  
  12. //     01/29/97    TWB    Integrated changes from Macintosh
  13. //    05/31/97    RKM    Updated to support Java 1.1
  14. //                    Made properties bound & constrained
  15. //  07/15/97    CAR marked fields transient as needed
  16.  
  17. /**
  18.  * MovingAnimation component.  Draws a series of images at sequential horizontal
  19.  * positions within the component area.  Each frame is drawn at a given offset
  20.  * from the previous frame.
  21.  *
  22.  * @see Animator
  23.  * @version 1.0, Nov 26, 1996
  24.  * @author Symantec
  25.  */
  26.  
  27.  
  28. public class MovingAnimation
  29.     extends Animator
  30.     implements Runnable
  31. {
  32.     /**
  33.      * Index of the image to draw. A loop counter.
  34.      */
  35.     transient protected int loopslot;
  36.     /**
  37.      * The amount each image is shifted horizontally from the previous image.
  38.      * The offset may be positive
  39.      * or negative. If the offset is positive, the first image is drawn on the
  40.      * left side of the component and subsequent images are drawn to the right
  41.      * of the first image by offset pixels.  If the offset is negative, the
  42.      * first image is drawn at the right side of the component area.
  43.      */
  44.     protected int shiftOffset;
  45.  
  46.     /**
  47.      * Constructs a default MovingAnimation.
  48.      */
  49.     public MovingAnimation()
  50.     {
  51.         shiftOffset    = 10;
  52.  
  53.         loopslot    = 0;
  54.         curXOffset    = 0;
  55.         forever        = true;
  56.  
  57.         vetos        = new symantec.itools.beans.VetoableChangeSupport(this);
  58.         changes        = new symantec.itools.beans.PropertyChangeSupport(this);
  59.     }
  60.  
  61.     /**
  62.      * Sets the animation shift offset.  Each image is shifted horizontally
  63.      * from the previous image by the specified offset.  Offset may be positive
  64.      * or negative. If the offset is positive, the first image is drawn on the
  65.      * left side of the component and subsequent images are drawn to the right
  66.      * of the first image by offset pixels.  If the offset is negative, the
  67.      * first image is drawn at the right side of the component area.
  68.      * @param newShiftOffset shift offset amount, in pixels
  69.      * @see #getShiftOffset
  70.      *
  71.      * @exception PropertyVetoException
  72.      * if the specified property value is unacceptable
  73.      */
  74.     public void setShiftOffset(int newShiftOffset)
  75.         throws PropertyVetoException
  76.     {
  77.         if (shiftOffset != newShiftOffset)
  78.         {
  79.             Integer oldShiftOffsetInt = new Integer(shiftOffset);
  80.             Integer newShiftOffsetInt = new Integer(newShiftOffset);
  81.  
  82.             vetos.fireVetoableChange("ShiftOffset",oldShiftOffsetInt,newShiftOffsetInt);
  83.  
  84.             shiftOffset = newShiftOffset;
  85.  
  86.             changes.firePropertyChange("ShiftOffset",oldShiftOffsetInt,newShiftOffsetInt);
  87.         }
  88.     }
  89.  
  90.     /**
  91.      * Gets the animation shift offset.
  92.      * @return int current shift offset amount, in pixels
  93.      * @see #setShiftOffset
  94.      */
  95.     public int getShiftOffset()
  96.     {
  97.         return shiftOffset;
  98.     }
  99.  
  100.     /**
  101.      * Body of MovingAnimation Thread.  This method is called by the Java Virtual Machine
  102.      * in response to a call to the start method of this object.
  103.      */
  104.     public void run()
  105.     {
  106.         Thread.currentThread().setPriority(Thread.NORM_PRIORITY - 1);
  107.  
  108.         Dimension d = size();
  109.  
  110.         int imageCount = images.size();
  111.  
  112.         if (imageCount > 1)
  113.         {
  114.             if (shiftOffset < 0)
  115.                 curXOffset = d.width - maxWidth;
  116.  
  117.             int count = 0;
  118.  
  119.             while (true)
  120.             {
  121.                 d = size();        //Needed if the component resizes while running.
  122.  
  123.                 if (++loopslot >= imageCount)
  124.                 {
  125.                     if (++count > numLoops && !forever)
  126.                         break;
  127.  
  128.                     loopslot = 0;
  129.                     curXOffset += shiftOffset;
  130.  
  131.                     if (curXOffset < 0)
  132.                         curXOffset = d.width - maxWidth;
  133.  
  134.                     else if (curXOffset + maxWidth > d.width)
  135.                         curXOffset = 0;
  136.                 }
  137.  
  138.                 repaint();
  139.  
  140.                 try
  141.                 {
  142.                     Thread.sleep(delay);
  143.                 }
  144.                 catch (InterruptedException e)
  145.                 {
  146.                     break;
  147.                 }
  148.             }
  149.         }
  150.     }
  151.  
  152.     /**
  153.      * Incrementally updates an image as image data becomes available.
  154.      * This is a standard Java AWT method which gets called by the AWT to
  155.      * incrementally draw an image as more image data becomes available.
  156.      * @param img the image being drawn
  157.      * @param flags image update flags (see class ImageObserver)
  158.      * @param x horizontal position
  159.      * @param y vertical position
  160.      * @param w width
  161.      * @param h height
  162.      *
  163.      * @return true if image has been completely loaded
  164.      *
  165.      * @see java.awt.image.ImageObserver
  166.      * @see java.awt.image.ImageObserver#imageUpdate
  167.      */
  168.     public boolean imageUpdate(Image img, int flags, int x, int y, int w, int h)
  169.     {
  170.         if ((flags & (SOMEBITS|FRAMEBITS|ALLBITS)) != 0)
  171.             if ((images != null) && (loopslot < images.size()) && (((AnimatorImage)images.elementAt(loopslot)).image == img))
  172.                 repaint(100);
  173.  
  174.         return (flags & (ALLBITS|ERROR)) == 0;
  175.     }
  176.  
  177.     /**
  178.      * Paints this component using the given graphics context.
  179.      * This is a standard Java AWT method which typically gets called
  180.      * by the AWT to handle painting this component. It paints this component
  181.      * using the given graphics context. The graphics context clipping region
  182.      * is set to the bounding rectangle of this component and its <0,0>
  183.      * coordinate is this component's top-left corner.
  184.      *
  185.      * The horizontal position of the image is shifted according to the current offset.
  186.      *
  187.      * @param g the graphics context used for painting
  188.      * @see java.awt.Component#repaint
  189.      * @see java.awt.Component#update
  190.      */
  191.     public void paint(Graphics g)
  192.     {
  193.         if ((images != null) && (loopslot < images.size()))
  194.         {
  195.             Image img = ((AnimatorImage)images.elementAt(loopslot)).image;
  196.             if (img != null)
  197.                 g.drawImage(img, curXOffset, 0, this);
  198.         }
  199.     }
  200.  
  201.     /**
  202.      * Adds a listener for all event changes.
  203.      * @param PropertyChangeListener listener the listener to add.
  204.      * @see #removePropertyChangeListener
  205.      */
  206.     public void addPropertyChangeListener(PropertyChangeListener listener)
  207.     {
  208.         super.addPropertyChangeListener(listener);
  209.         changes.addPropertyChangeListener(listener);
  210.     }
  211.  
  212.     /**
  213.      * Removes a listener for all event changes.
  214.      * @param PropertyChangeListener listener the listener to remove.
  215.      * @see #addPropertyChangeListener
  216.      */
  217.     public void removePropertyChangeListener(PropertyChangeListener listener)
  218.     {
  219.         super.removePropertyChangeListener(listener);
  220.         changes.removePropertyChangeListener(listener);
  221.     }
  222.  
  223.     /**
  224.      * Adds a vetoable listener for all event changes.
  225.      * @param VetoableChangeListener listener the listener to add.
  226.      * @see #removeVetoableChangeListener
  227.      */
  228.     public void addVetoableChangeListener(VetoableChangeListener listener)
  229.     {
  230.         super.addVetoableChangeListener(listener);
  231.         vetos.addVetoableChangeListener(listener);
  232.     }
  233.  
  234.     /**
  235.      * Removes a vetoable listener for all event changes.
  236.      * @param VetoableChangeListener listener the listener to remove.
  237.      * @see #addVetoableChangeListener
  238.      */
  239.     public void removeVetoableChangeListener(VetoableChangeListener listener)
  240.     {
  241.         super.removeVetoableChangeListener(listener);
  242.         vetos.removeVetoableChangeListener(listener);
  243.     }
  244.  
  245.     // Private members
  246.     transient private int curXOffset;
  247.     private symantec.itools.beans.VetoableChangeSupport vetos;
  248.     private symantec.itools.beans.PropertyChangeSupport changes;
  249. }
  250.